﻿using Basic.WxModel.WxPlatform;
using Basic.WxRepository;
using WeDonekRpc.CacheClient.Interface;
using WeDonekRpc.Helper;

namespace Basic.WxCollect.lmpl
{
    internal class WxPlatformCollect : IWxPlatformCollect
    {
        private readonly IWxPlatformRepository _Platform;
        private readonly ICacheController _Cache;

        public WxPlatformCollect (IWxPlatformRepository platform, ICacheController cache)
        {
            this._Platform = platform;
            this._Cache = cache;
        }

        public WxPlatformDto GetPlatform (string appId)
        {
            string key = "WxPlatform_" + appId;
            if (!this._Cache.TryGet(key, out WxPlatformDto dto))
            {
                dto = this._Platform.FindPlatform<WxPlatformDto>(appId);
                if (dto == null)
                {
                    _ = this._Cache.Add(key, dto, new TimeSpan(0, 1, 0));
                }
                else
                {
                    _ = this._Cache.Add(key, dto);
                }
            }
            if (dto == null)
            {
                throw new ErrorException("wx.platform.not.find");
            }
            return dto;
        }

    }
}
